package com.solution.wx.share.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.solution.common.dao.BaseDao;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.redis.RedisCacheClient;
import com.solution.common.service.impl.BaseServiceImpl;
import com.solution.common.utils.string.StringUtil;
import com.solution.wx.share.constants.ShareConstants.COMMENT_TYPE;
import com.solution.wx.share.dao.ShareCommentDao;
import com.solution.wx.share.entity.ShareCommentEntity;
import com.solution.wx.share.entity.ShareCommentImageEntity;
import com.solution.wx.share.service.ShareCommentService;

/**
 * 用户评论Service实现
 * @author xkl
 * @date 2018年5月8日 下午4:08:17
 */
@Service
public class ShareCommentServiceImpl extends BaseServiceImpl<ShareCommentEntity> implements ShareCommentService {
	
	@Autowired
	private ShareCommentDao shareCommentDao;
	
	@Autowired
	RedisCacheClient redisCacheClient;
	
	/**
	 * 评论点赞数
	 */
	private static final String COMMENT_LIKE_QUANTITY = "COMMENT_LIKE_QUANTITY";
	
	/**
	 * 用户已点赞评论
	 */
	private static final String USER_COMMENT_LIKE = "USER_COMMENT_LIKE_";
	
	/**
	 * 分享信息变化的分享
	 */
	private static final String SHARE_INTERACTION_CHANGE = "SHARE_INTERACTION_CHANGE";
	
	@Override
	protected BaseDao<ShareCommentEntity> getDao() {
		return this.shareCommentDao;
	}

	/**
	 * 根据ID获取评论
	 * @param commentId
	 * @return
	 * @author xkl
	 * @date 2018年5月8日 下午4:17:34
	 */
	@Override
	public ShareCommentEntity getShareCommentById(Long commentId) {
		return shareCommentDao.getById(commentId);
	}
	
	/**
	 * 获取分享信息评论分页列表
	 * @param pageParam
	 * @param param
	 * @return
	 * @author xkl
	 * @date 2018年5月8日 下午4:17:20
	 */
	@Override
	public PageBean listShareCommentPage(PageParam pageParam, Map<String, Object> param) {
		return shareCommentDao.listPage(pageParam, param);
	}

	/**
	 * 获取评论分页列表并包含3条子评论
	 * @param pageParam
	 * @param param
	 * @return
	 * @author xkl
	 * @date 2018年5月8日 下午5:30:08
	 */
	@Override
	public PageBean listShareCommentPageWithReply(PageParam pageParam, Map<String, Object> param) {
		PageBean parentPageBean = shareCommentDao.listPage(pageParam, param);
		param.put("orderBy", "COMMENT_LIKE_QUANTITY");
		param.put("sort", "DESC");
		param.put("offest", 0);
		param.put("limit", 3);
		for (int i = 0; i < parentPageBean.getRecordList().size(); i++) {
			ShareCommentEntity commentEntity = (ShareCommentEntity)parentPageBean.getRecordList().get(i);
			param.put("parentCommentId", commentEntity.getId());
			//查询子回复
			List<ShareCommentEntity> replyList = shareCommentDao.listBy(param);
			commentEntity.setReplyList(replyList);
			Long commentLikeQuantity = redisCacheClient.getHashValue(COMMENT_LIKE_QUANTITY, commentEntity.getId().toString());
			commentEntity.setCommentLikeQuantity(commentLikeQuantity);
		}
		return parentPageBean;
	}

	/**
	 * 用户对评论点赞
	 * @param commentId
	 * @param customerId
	 * @return
	 * @author xkl
	 * @date 2018年5月9日 下午5:57:46
	 */
	@Override
	public Long doShareCommentLike(Long commentId, Long customerId) {
		//评论赞数+1
		redisCacheClient.doHashIncrby(COMMENT_LIKE_QUANTITY, commentId.toString());
		//记录分享评论信息变化
		redisCacheClient.doHashIncrby(SHARE_INTERACTION_CHANGE, "COMMENT_"+commentId);
		//记录用户评论点赞
		return redisCacheClient.doHashIncrby(USER_COMMENT_LIKE + customerId, commentId.toString());
	}

	/**
	 * 用户是否对该评论点赞
	 * @param commentId
	 * @param customerId
	 * @return
	 * @author xkl
	 * @date 2018年5月9日 下午6:04:43
	 */
	@Override
	public Long isUserCommentLike(Long commentId, Long customerId) {
		return redisCacheClient.getHashValue(USER_COMMENT_LIKE + customerId, commentId.toString());
	}

	/**
	 * 保存客户分享评论
	 * 
	 * @param shareCommentEntity
	 *            客户分享评论实体
	 * @return Long 添加成功客户分享评论的ID
	 * @author xu_cc
	 * @date 2018-05-07 15:23:32
	 */
	@Override
	@Transactional
	public Long saveShareComment(ShareCommentEntity shareCommentEntity) {
		// 评论插入
		long commentId = shareCommentDao.saveShareComment(shareCommentEntity);
		// 评论图片插入
		if (StringUtil.isNotNull(shareCommentEntity.getImageUrlStr())) {
			String[] imageUrlArr = shareCommentEntity.getImageUrlStr().split(",");
			List<ShareCommentImageEntity> imageList = new ArrayList<ShareCommentImageEntity>();
			ShareCommentImageEntity commentImage = null;
			for (String imageUrl : imageUrlArr) {
				if (StringUtil.isNotNull(imageUrl)) {
					commentImage = new ShareCommentImageEntity();
					commentImage.setCommentId(commentId);
					commentImage.setImageUrl(imageUrl);
					imageList.add(commentImage);
				}
			}
			if (imageList.size() > 0) {
				shareCommentDao.saveCommentImageList(imageList);
			}
		}
		//如果是回复则主题回复数量+1
		if (COMMENT_TYPE.COMMENT_REPLY.getCode().equals(shareCommentEntity.getCommentType())) {
			shareCommentDao.doCommentReplyIncrBy(shareCommentEntity.getParentCommentId());
		}
		return 1L;
	}

	/**
	 * 更新用户
	 * 
	 * @param shareCommentEntity
	 *            用户实体
	 * @return Long 更新是否成功，>1代表更新成功
	 * @author xu_cc
	 * @date 2018-05-07 15:23:32
	 */
	@Override
	public int updateShareComment(ShareCommentEntity shareCommentEntity) {
		return shareCommentDao.updateShareComment(shareCommentEntity);
	}

	/**
	 * 获取评论点赞数
	 * @param commentId
	 * @return
	 * @author xkl
	 * @date 2018年5月24日 下午2:19:49
	 */
	@Override
	public Long getShareCommentLikeQuantity(Long commentId) {
		return redisCacheClient.getHashValue(COMMENT_LIKE_QUANTITY, commentId.toString());
	}

	/**
	 * 批量更新评论点赞数
	 * @param commentList
	 * @return
	 * @author xkl
	 * @date 2018年5月24日 下午2:36:33
	 */
	@Override
	public Integer updateShareCommentLikeQuantity(List<ShareCommentEntity> commentList) {
		return shareCommentDao.updateShareCommentLikeQuantity(commentList);
	}

	/**
	 * 根据条件查询客户分享评论
	 * 
	 * @param param
	 * @return 数据列表
	 * @author xu_cc
	 * @date 2018-05-07 15:23:32
	 */
	@Override
	public List<ShareCommentEntity> listShareComment(Map<String, Object> param) {
		return shareCommentDao.listShareComment(param);
	}

	/**
	 * 删除客户分享评论
	 * 
	 * @param
	 * @return int 返回删除记录条数
	 * @author xu_cc
	 * @date 2018-05-07 15:23:32
	 */
	public int deleteShareCommentById(long id) {
		return shareCommentDao.deleteById(id);
	}

	/**
	 * 客户端评论展示
	 */
	public PageBean listShowCommentPage(PageParam pageParam, Map<String, Object> param) {
		PageBean parentPageBean = shareCommentDao.listShowCommentPage(pageParam, param);
		Long customerId = StringUtil.isNotNull(param.get("customerId")) ? Long.parseLong(param.get("customerId").toString()) : 0L;
		for (int i = 0; i < parentPageBean.getRecordList().size(); i++) {
			//父级回复
			ShareCommentEntity commentEntity = (ShareCommentEntity)parentPageBean.getRecordList().get(i);
			Long commentLikeQuantity = redisCacheClient.getHashValue(COMMENT_LIKE_QUANTITY, commentEntity.getId().toString());
			Integer isUserCommentLike = isUserCommentLike(commentEntity.getId(), customerId) > 0 ? 1 : 0;
			commentEntity.setIsUserLike(isUserCommentLike);
			commentEntity.setCommentLikeQuantity(commentLikeQuantity);
			//子级回复
			List<ShareCommentEntity> replyList = commentEntity.getReplyList();
			for (ShareCommentEntity entity : replyList) {
				Long replyLikeQuantity = redisCacheClient.getHashValue(COMMENT_LIKE_QUANTITY, entity.getId().toString());
				Integer isUserreplyLike = isUserCommentLike(entity.getId(), customerId) > 0 ? 1 : 0;
				entity.setIsUserLike(isUserreplyLike);
				entity.setCommentLikeQuantity(replyLikeQuantity);
			}
		}
		return parentPageBean;
	}
}
